package com.xposed.openfire.smack.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xposed.openfire.smack.R;
import com.xposed.openfire.smack.entity.FriendAdd;
import com.xposed.openfire.smack.entity.FriendGood;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserAddAdapter extends RecyclerView.Adapter<UserAddAdapter.UserAddViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<FriendAdd> friends;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public UserAddAdapter(Context mContext, List<FriendAdd> friends) {
        this.mContext = mContext;
        this.friends = friends;
    }

    @NonNull
    @Override
    public UserAddViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_user_add, viewGroup, (false));
        inflate.setOnClickListener(this);
        return new UserAddViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final UserAddViewHolder viewHolder, int position) {
        viewHolder.tvName.setText(friends.get(position).getFrom());
        viewHolder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return friends.size();
    }

    /**
     * 加载数据
     */
    public void addData(List<FriendAdd> friends) {
        this.friends.clear();
        this.friends.addAll(friends);
        notifyDataSetChanged();
    }

    /**
     * 加载数据
     */
    public void addItem(FriendAdd friend) {
        this.friends.add(getItemCount(), friend);
        notifyItemChanged(getItemCount());
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null)
            onItemClickListener.onItemClick(v, (int) v.getTag());
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);
    }

    class UserAddViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tvName;

        UserAddViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind((this), itemView);
        }
    }
}
